home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / CBKEYPRE.C < prev    next >
Text File  |  1991-09-23  |  3KB  |  147 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)cbkeypre.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STDLIB
  11. #include <stdlib.h>
  12. #endif
  13. #ifdef AC_STRING
  14. #include <string.h>
  15. #endif
  16.  
  17. /* library headers */
  18. #include <blkio.h>
  19. #include <btree.h>
  20. #include <lseq.h>
  21.  
  22. /* local headers */
  23. #include "cbase_.h"
  24.  
  25. /*man---------------------------------------------------------------------------
  26. NAME
  27.      cbkeyprev - previous cbase key
  28.  
  29. SYNOPSIS
  30.      #include <cbase.h>
  31.  
  32.      int cbkeyprev(cbp, field)
  33.      cbase_t *cbp;
  34.      int field;
  35.  
  36. DESCRIPTION
  37.      The cbkeyprev function retreats the key cursor of the specified
  38.      field in cbase cbp by one.  The record cursor of cbp is
  39.      positioned to the record associated with that key.  Other key
  40.      cursors are not affected.
  41.  
  42.      cbkeyprev will fail if one or more of the following is true:
  43.  
  44.      [EINVAL]       cbp is not a valid cbase pointer.
  45.      [EINVAL]       field is not a valid field number for
  46.                     cbase cbp.
  47.      [CBELOCK]      cbp is not locked.
  48.      [CBENKEY]      field is not a key.
  49.      [CBENOPEN]     cbp is not open.
  50.  
  51. SEE ALSO
  52.      cbkcursor, cbkeyfirst, cbkeylast, cbkeynext, cbrecprev.
  53.  
  54. DIAGNOSTICS
  55.      Upon successful completion, a value of 0 is returned.  Otherwise,
  56.      a value of -1 is returned, and errno set to indicate the error.
  57.  
  58. ------------------------------------------------------------------------------*/
  59. #ifdef AC_PROTO
  60. int cbkeyprev(cbase_t *cbp, int field)
  61. #else
  62. int cbkeyprev(cbp, field)
  63. cbase_t *cbp;
  64. int field;
  65. #endif
  66. {
  67.     void *    buf    = NULL;
  68.     cbrpos_t cbrpos    = NIL;
  69.     lspos_t    lspos    = NIL;
  70.  
  71.     /* validate arguments */
  72.     if (!cb_valid(cbp)) {
  73.         errno = EINVAL;
  74.         return -1;
  75.     }
  76.  
  77.     /* check if not open */
  78.     if (!(cbp->flags & CBOPEN)) {
  79.         errno = CBENOPEN;
  80.         return -1;
  81.     }
  82.  
  83.     /* validate arguments */
  84.     if (field < 0 || field >= cbp->fldc) {
  85.         errno = EINVAL;
  86.         return -1;
  87.     }
  88.  
  89.     /* check if field not a key */
  90.     if (!(cbp->fldv[field].flags & CB_FKEY)) {
  91.         errno = CBENKEY;
  92.         return -1;
  93.     }
  94.  
  95.     /* check if not locked */
  96.     if (!(cbp->flags & CBLOCKS)) {
  97.         errno = CBELOCK;
  98.         return -1;
  99.     }
  100.  
  101.     /* set key cursor to previous key */
  102.     if (btprev(cbp->btpv[field]) == -1) {
  103.         CBEPRINT;
  104.         return -1;
  105.     }
  106.  
  107.     /* check if cursor is null */
  108.     if (btcursor(cbp->btpv[field]) == NULL) {
  109.         /* set record cursor to null */
  110.         if (lssetcur(cbp->lsp, NULL) == -1) {
  111.             CBEPRINT;
  112.             return -1;
  113.         }
  114.         return 0;
  115.     }
  116.  
  117.     /* get record position */
  118.     if (btkeysize(cbp->btpv[field]) != (cbp->fldv[field].len + sizeof(cbrpos_t))) {
  119.         CBEPRINT;
  120.         errno = CBEPANIC;
  121.         return -1;
  122.     }
  123.     buf = calloc((size_t)1, btkeysize(cbp->btpv[field]));
  124.     if (buf == NULL) {
  125.         CBEPRINT;
  126.         errno = ENOMEM;
  127.         return -1;
  128.     }
  129.     if (btgetk(cbp->btpv[field], buf) == -1) {
  130.         CBEPRINT;
  131.         free(buf);
  132.         return -1;
  133.     }
  134.     memcpy(&cbrpos, (char *)buf + cbp->fldv[field].len, sizeof(cbrpos));
  135.     free(buf);
  136.     buf = NULL;
  137.  
  138.     /* set record cursor */
  139.     lspos = cbrpos;
  140.     if (lssetcur(cbp->lsp, &lspos) == -1) {
  141.         CBEPRINT;
  142.         return -1;
  143.     }
  144.  
  145.     return 0;
  146. }
  147.